1 /* 2 * Copyright 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /** 18 * @file hardware_buffer.h 19 * @brief API for native hardware buffers. 20 */ 21 /** 22 * @defgroup AHardwareBuffer Native Hardware Buffer 23 * 24 * AHardwareBuffer objects represent chunks of memory that can be 25 * accessed by various hardware components in the system. It can be 26 * easily converted to the Java counterpart 27 * android.hardware.HardwareBuffer and passed between processes using 28 * Binder. All operations involving AHardwareBuffer and HardwareBuffer 29 * are zero-copy, i.e., passing AHardwareBuffer to another process 30 * creates a shared view of the same region of memory. 31 * 32 * AHardwareBuffers can be bound to EGL/OpenGL and Vulkan primitives. 33 * For EGL, use the extension function eglGetNativeClientBufferANDROID 34 * to obtain an EGLClientBuffer and pass it directly to 35 * eglCreateImageKHR. Refer to the EGL extensions 36 * EGL_ANDROID_get_native_client_buffer and 37 * EGL_ANDROID_image_native_buffer for more information. In Vulkan, 38 * the contents of the AHardwareBuffer can be accessed as external 39 * memory. See the VK_ANDROID_external_memory_android_hardware_buffer 40 * extension for details. 41 * 42 * @{ 43 */ 44 45 module android.ndk.hardware_buffer; 46 47 import arsd.jni; 48 import android.ndk; 49 50 extern (C): 51 nothrow: 52 @nogc: 53 54 /** 55 * Buffer pixel formats. 56 */ 57 enum AHardwareBuffer_Format 58 { 59 /** 60 * Corresponding formats: 61 * Vulkan: VK_FORMAT_R8G8B8A8_UNORM 62 * OpenGL ES: GL_RGBA8 63 */ 64 AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM = 1, 65 66 /** 67 * 32 bits per pixel, 8 bits per channel format where alpha values are 68 * ignored (always opaque). 69 * Corresponding formats: 70 * Vulkan: VK_FORMAT_R8G8B8A8_UNORM 71 * OpenGL ES: GL_RGB8 72 */ 73 AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM = 2, 74 75 /** 76 * Corresponding formats: 77 * Vulkan: VK_FORMAT_R8G8B8_UNORM 78 * OpenGL ES: GL_RGB8 79 */ 80 AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM = 3, 81 82 /** 83 * Corresponding formats: 84 * Vulkan: VK_FORMAT_R5G6B5_UNORM_PACK16 85 * OpenGL ES: GL_RGB565 86 */ 87 AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM = 4, 88 89 /** 90 * Corresponding formats: 91 * Vulkan: VK_FORMAT_R16G16B16A16_SFLOAT 92 * OpenGL ES: GL_RGBA16F 93 */ 94 AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT = 0x16, 95 96 /** 97 * Corresponding formats: 98 * Vulkan: VK_FORMAT_A2B10G10R10_UNORM_PACK32 99 * OpenGL ES: GL_RGB10_A2 100 */ 101 AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM = 0x2b, 102 103 /** 104 * Opaque binary blob format. 105 * Must have height 1 and one layer, with width equal to the buffer 106 * size in bytes. Corresponds to Vulkan buffers and OpenGL buffer 107 * objects. Can be bound to the latter using GL_EXT_external_buffer. 108 */ 109 AHARDWAREBUFFER_FORMAT_BLOB = 0x21, 110 111 /** 112 * Corresponding formats: 113 * Vulkan: VK_FORMAT_D16_UNORM 114 * OpenGL ES: GL_DEPTH_COMPONENT16 115 */ 116 AHARDWAREBUFFER_FORMAT_D16_UNORM = 0x30, 117 118 /** 119 * Corresponding formats: 120 * Vulkan: VK_FORMAT_X8_D24_UNORM_PACK32 121 * OpenGL ES: GL_DEPTH_COMPONENT24 122 */ 123 AHARDWAREBUFFER_FORMAT_D24_UNORM = 0x31, 124 125 /** 126 * Corresponding formats: 127 * Vulkan: VK_FORMAT_D24_UNORM_S8_UINT 128 * OpenGL ES: GL_DEPTH24_STENCIL8 129 */ 130 AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT = 0x32, 131 132 /** 133 * Corresponding formats: 134 * Vulkan: VK_FORMAT_D32_SFLOAT 135 * OpenGL ES: GL_DEPTH_COMPONENT32F 136 */ 137 AHARDWAREBUFFER_FORMAT_D32_FLOAT = 0x33, 138 139 /** 140 * Corresponding formats: 141 * Vulkan: VK_FORMAT_D32_SFLOAT_S8_UINT 142 * OpenGL ES: GL_DEPTH32F_STENCIL8 143 */ 144 AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT = 0x34, 145 146 /** 147 * Corresponding formats: 148 * Vulkan: VK_FORMAT_S8_UINT 149 * OpenGL ES: GL_STENCIL_INDEX8 150 */ 151 AHARDWAREBUFFER_FORMAT_S8_UINT = 0x35, 152 153 /** 154 * YUV 420 888 format. 155 * Must have an even width and height. Can be accessed in OpenGL 156 * shaders through an external sampler. Does not support mip-maps 157 * cube-maps or multi-layered textures. 158 */ 159 AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420 = 0x23 160 } 161 162 /** 163 * Buffer usage flags, specifying how the buffer will be accessed. 164 */ 165 enum AHardwareBuffer_UsageFlags 166 { 167 /// The buffer will never be locked for direct CPU reads using the 168 /// AHardwareBuffer_lock() function. Note that reading the buffer 169 /// using OpenGL or Vulkan functions or memory mappings is still 170 /// allowed. 171 AHARDWAREBUFFER_USAGE_CPU_READ_NEVER = 0UL, 172 /// The buffer will sometimes be locked for direct CPU reads using 173 /// the AHardwareBuffer_lock() function. Note that reading the 174 /// buffer using OpenGL or Vulkan functions or memory mappings 175 /// does not require the presence of this flag. 176 AHARDWAREBUFFER_USAGE_CPU_READ_RARELY = 2UL, 177 /// The buffer will often be locked for direct CPU reads using 178 /// the AHardwareBuffer_lock() function. Note that reading the 179 /// buffer using OpenGL or Vulkan functions or memory mappings 180 /// does not require the presence of this flag. 181 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN = 3UL, 182 /// CPU read value mask. 183 AHARDWAREBUFFER_USAGE_CPU_READ_MASK = 0xFUL, 184 185 /// The buffer will never be locked for direct CPU writes using the 186 /// AHardwareBuffer_lock() function. Note that writing the buffer 187 /// using OpenGL or Vulkan functions or memory mappings is still 188 /// allowed. 189 AHARDWAREBUFFER_USAGE_CPU_WRITE_NEVER = 0UL << 4, 190 /// The buffer will sometimes be locked for direct CPU writes using 191 /// the AHardwareBuffer_lock() function. Note that writing the 192 /// buffer using OpenGL or Vulkan functions or memory mappings 193 /// does not require the presence of this flag. 194 AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY = 2UL << 4, 195 /// The buffer will often be locked for direct CPU writes using 196 /// the AHardwareBuffer_lock() function. Note that writing the 197 /// buffer using OpenGL or Vulkan functions or memory mappings 198 /// does not require the presence of this flag. 199 AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN = 3UL << 4, 200 /// CPU write value mask. 201 AHARDWAREBUFFER_USAGE_CPU_WRITE_MASK = 0xFUL << 4, 202 203 /// The buffer will be read from by the GPU as a texture. 204 AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE = 1UL << 8, 205 /// The buffer will be written to by the GPU as a framebuffer attachment. 206 AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER = 1UL << 9, 207 /** 208 * The buffer will be written to by the GPU as a framebuffer 209 * attachment. 210 * 211 * Note that the name of this flag is somewhat misleading: it does 212 * not imply that the buffer contains a color format. A buffer with 213 * depth or stencil format that will be used as a framebuffer 214 * attachment should also have this flag. Use the equivalent flag 215 * AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER to avoid this confusion. 216 */ 217 AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT = AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER, 218 /** 219 * The buffer will be used as a composer HAL overlay layer. 220 * 221 * This flag is currently only needed when using ASurfaceTransaction_setBuffer 222 * to set a buffer. In all other cases, the framework adds this flag 223 * internally to buffers that could be presented in a composer overlay. 224 * ASurfaceTransaction_setBuffer is special because it uses buffers allocated 225 * directly through AHardwareBuffer_allocate instead of buffers allocated 226 * by the framework. 227 */ 228 AHARDWAREBUFFER_USAGE_COMPOSER_OVERLAY = 1UL << 11, 229 /** 230 * The buffer is protected from direct CPU access or being read by 231 * non-secure hardware, such as video encoders. 232 * 233 * This flag is incompatible with CPU read and write flags. It is 234 * mainly used when handling DRM video. Refer to the EGL extension 235 * EGL_EXT_protected_content and GL extension 236 * GL_EXT_protected_textures for more information on how these 237 * buffers are expected to behave. 238 */ 239 AHARDWAREBUFFER_USAGE_PROTECTED_CONTENT = 1UL << 14, 240 /// The buffer will be read by a hardware video encoder. 241 AHARDWAREBUFFER_USAGE_VIDEO_ENCODE = 1UL << 16, 242 /** 243 * The buffer will be used for direct writes from sensors. 244 * When this flag is present, the format must be AHARDWAREBUFFER_FORMAT_BLOB. 245 */ 246 AHARDWAREBUFFER_USAGE_SENSOR_DIRECT_DATA = 1UL << 23, 247 /** 248 * The buffer will be used as a shader storage or uniform buffer object. 249 * When this flag is present, the format must be AHARDWAREBUFFER_FORMAT_BLOB. 250 */ 251 AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER = 1UL << 24, 252 /** 253 * The buffer will be used as a cube map texture. 254 * When this flag is present, the buffer must have a layer count 255 * that is a multiple of 6. Note that buffers with this flag must be 256 * bound to OpenGL textures using the extension 257 * GL_EXT_EGL_image_storage instead of GL_KHR_EGL_image. 258 */ 259 AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP = 1UL << 25, 260 /** 261 * The buffer contains a complete mipmap hierarchy. 262 * Note that buffers with this flag must be bound to OpenGL textures using 263 * the extension GL_EXT_EGL_image_storage instead of GL_KHR_EGL_image. 264 */ 265 AHARDWAREBUFFER_USAGE_GPU_MIPMAP_COMPLETE = 1UL << 26, 266 267 AHARDWAREBUFFER_USAGE_VENDOR_0 = 1UL << 28, 268 AHARDWAREBUFFER_USAGE_VENDOR_1 = 1UL << 29, 269 AHARDWAREBUFFER_USAGE_VENDOR_2 = 1UL << 30, 270 AHARDWAREBUFFER_USAGE_VENDOR_3 = 1UL << 31, 271 AHARDWAREBUFFER_USAGE_VENDOR_4 = 1UL << 48, 272 AHARDWAREBUFFER_USAGE_VENDOR_5 = 1UL << 49, 273 AHARDWAREBUFFER_USAGE_VENDOR_6 = 1UL << 50, 274 AHARDWAREBUFFER_USAGE_VENDOR_7 = 1UL << 51, 275 AHARDWAREBUFFER_USAGE_VENDOR_8 = 1UL << 52, 276 AHARDWAREBUFFER_USAGE_VENDOR_9 = 1UL << 53, 277 AHARDWAREBUFFER_USAGE_VENDOR_10 = 1UL << 54, 278 AHARDWAREBUFFER_USAGE_VENDOR_11 = 1UL << 55, 279 AHARDWAREBUFFER_USAGE_VENDOR_12 = 1UL << 56, 280 AHARDWAREBUFFER_USAGE_VENDOR_13 = 1UL << 57, 281 AHARDWAREBUFFER_USAGE_VENDOR_14 = 1UL << 58, 282 AHARDWAREBUFFER_USAGE_VENDOR_15 = 1UL << 59, 283 AHARDWAREBUFFER_USAGE_VENDOR_16 = 1UL << 60, 284 AHARDWAREBUFFER_USAGE_VENDOR_17 = 1UL << 61, 285 AHARDWAREBUFFER_USAGE_VENDOR_18 = 1UL << 62, 286 AHARDWAREBUFFER_USAGE_VENDOR_19 = 1UL << 63 287 } 288 289 /** 290 * Buffer description. Used for allocating new buffers and querying 291 * parameters of existing ones. 292 */ 293 struct AHardwareBuffer_Desc 294 { 295 uint width; ///< Width in pixels. 296 uint height; ///< Height in pixels. 297 /** 298 * Number of images in an image array. AHardwareBuffers with one 299 * layer correspond to regular 2D textures. AHardwareBuffers with 300 * more than layer correspond to texture arrays. If the layer count 301 * is a multiple of 6 and the usage flag 302 * AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP is present, the buffer is 303 * a cube map or a cube map array. 304 */ 305 uint layers; 306 uint format; ///< One of AHardwareBuffer_Format. 307 ulong usage; ///< Combination of AHardwareBuffer_UsageFlags. 308 uint stride; ///< Row stride in pixels, ignored for AHardwareBuffer_allocate() 309 uint rfu0; ///< Initialize to zero, reserved for future use. 310 ulong rfu1; ///< Initialize to zero, reserved for future use. 311 } 312 313 /** 314 * Holds data for a single image plane. 315 */ 316 struct AHardwareBuffer_Plane 317 { 318 void* data; ///< Points to first byte in plane 319 uint pixelStride; ///< Distance in bytes from the color channel of one pixel to the next 320 uint rowStride; ///< Distance in bytes from the first value of one row of the image to 321 /// the first value of the next row. 322 } 323 324 /** 325 * Holds all image planes that contain the pixel data. 326 */ 327 struct AHardwareBuffer_Planes 328 { 329 uint planeCount; ///< Number of distinct planes 330 AHardwareBuffer_Plane[4] planes; ///< Array of image planes 331 } 332 333 /** 334 * Opaque handle for a native hardware buffer. 335 */ 336 struct AHardwareBuffer; 337 338 /** 339 * Allocates a buffer that matches the passed AHardwareBuffer_Desc. 340 * 341 * If allocation succeeds, the buffer can be used according to the 342 * usage flags specified in its description. If a buffer is used in ways 343 * not compatible with its usage flags, the results are undefined and 344 * may include program termination. 345 * 346 * \return 0 on success, or an error number of the allocation fails for 347 * any reason. The returned buffer has a reference count of 1. 348 */ 349 int AHardwareBuffer_allocate ( 350 const(AHardwareBuffer_Desc)* desc, 351 AHardwareBuffer** outBuffer); 352 /** 353 * Acquire a reference on the given AHardwareBuffer object. 354 * 355 * This prevents the object from being deleted until the last reference 356 * is removed. 357 */ 358 void AHardwareBuffer_acquire (AHardwareBuffer* buffer); 359 360 /** 361 * Remove a reference that was previously acquired with 362 * AHardwareBuffer_acquire() or AHardwareBuffer_allocate(). 363 */ 364 void AHardwareBuffer_release (AHardwareBuffer* buffer); 365 366 /** 367 * Return a description of the AHardwareBuffer in the passed 368 * AHardwareBuffer_Desc struct. 369 */ 370 void AHardwareBuffer_describe ( 371 const(AHardwareBuffer)* buffer, 372 AHardwareBuffer_Desc* outDesc); 373 374 /** 375 * Lock the AHardwareBuffer for direct CPU access. 376 * 377 * This function can lock the buffer for either reading or writing. 378 * It may block if the hardware needs to finish rendering, if CPU caches 379 * need to be synchronized, or possibly for other implementation- 380 * specific reasons. 381 * 382 * The passed AHardwareBuffer must have one layer, otherwise the call 383 * will fail. 384 * 385 * If \a fence is not negative, it specifies a fence file descriptor on 386 * which to wait before locking the buffer. If it's negative, the caller 387 * is responsible for ensuring that writes to the buffer have completed 388 * before calling this function. Using this parameter is more efficient 389 * than waiting on the fence and then calling this function. 390 * 391 * The \a usage parameter may only specify AHARDWAREBUFFER_USAGE_CPU_*. 392 * If set, then outVirtualAddress is filled with the address of the 393 * buffer in virtual memory. The flags must also be compatible with 394 * usage flags specified at buffer creation: if a read flag is passed, 395 * the buffer must have been created with 396 * AHARDWAREBUFFER_USAGE_CPU_READ_RARELY or 397 * AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN. If a write flag is passed, it 398 * must have been created with AHARDWAREBUFFER_USAGE_CPU_WRITE_RARELY or 399 * AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN. 400 * 401 * If \a rect is not NUL, the caller promises to modify only data in 402 * the area specified by rect. If rect is NUL, the caller may modify 403 * the contents of the entire buffer. The content of the buffer outside 404 * of the specified rect is NOT modified by this call. 405 * 406 * It is legal for several different threads to lock a buffer for read 407 * access; none of the threads are blocked. 408 * 409 * Locking a buffer simultaneously for write or read/write is undefined, 410 * but will neither terminate the process nor block the caller. 411 * AHardwareBuffer_lock may return an error or leave the buffer's 412 * content in an indeterminate state. 413 * 414 * If the buffer has AHARDWAREBUFFER_FORMAT_BLOB, it is legal lock it 415 * for reading and writing in multiple threads and/or processes 416 * simultaneously, and the contents of the buffer behave like shared 417 * memory. 418 * 419 * \return 0 on success. -EINVAL if \a buffer is NUL, the usage flags 420 * are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or the buffer 421 * has more than one layer. Error number if the lock fails for any other 422 * reason. 423 */ 424 int AHardwareBuffer_lock ( 425 AHardwareBuffer* buffer, 426 ulong usage, 427 int fence, 428 const(ARect)* rect, 429 void** outVirtualAddress); 430 431 /** 432 * Lock a potentially multi-planar AHardwareBuffer for direct CPU access. 433 * 434 * This function is similar to AHardwareBuffer_lock, but can lock multi-planar 435 * formats. The locked planes are returned in the \a outPlanes argument. Note, 436 * that multi-planar should not be confused with multi-layer images, which this 437 * locking function does not support. 438 * 439 * YUV formats are always represented by three separate planes of data, one for 440 * each color plane. The order of planes in the array is guaranteed such that 441 * plane #0 is always Y, plane #1 is always U (Cb), and plane #2 is always V 442 * (Cr). All other formats are represented by a single plane. 443 * 444 * Additional information always accompanies the buffers, describing the row 445 * stride and the pixel stride for each plane. 446 * 447 * In case the buffer cannot be locked, \a outPlanes will contain zero planes. 448 * 449 * See the AHardwareBuffer_lock documentation for all other locking semantics. 450 * 451 * \return 0 on success. -EINVAL if \a buffer is NUL, the usage flags 452 * are not a combination of AHARDWAREBUFFER_USAGE_CPU_*, or the buffer 453 * has more than one layer. Error number if the lock fails for any other 454 * reason. 455 */ 456 int AHardwareBuffer_lockPlanes ( 457 AHardwareBuffer* buffer, 458 ulong usage, 459 int fence, 460 const(ARect)* rect, 461 AHardwareBuffer_Planes* outPlanes); 462 463 /** 464 * Unlock the AHardwareBuffer from direct CPU access. 465 * 466 * Must be called after all changes to the buffer are completed by the 467 * caller. If \a fence is NUL, the function will block until all work 468 * is completed. Otherwise, \a fence will be set either to a valid file 469 * descriptor or to -1. The file descriptor will become signaled once 470 * the unlocking is complete and buffer contents are updated. 471 * The caller is responsible for closing the file descriptor once it's 472 * no longer needed. The value -1 indicates that unlocking has already 473 * completed before the function returned and no further operations are 474 * necessary. 475 * 476 * \return 0 on success. -EINVAL if \a buffer is NUL. Error number if 477 * the unlock fails for any reason. 478 */ 479 int AHardwareBuffer_unlock (AHardwareBuffer* buffer, int* fence); 480 481 /** 482 * Send the AHardwareBuffer to an AF_UNIX socket. 483 * 484 * \return 0 on success, -EINVAL if \a buffer is NUL, or an error 485 * number if the operation fails for any reason. 486 */ 487 int AHardwareBuffer_sendHandleToUnixSocket (const(AHardwareBuffer)* buffer, int socketFd); 488 489 /** 490 * Receive an AHardwareBuffer from an AF_UNIX socket. 491 * 492 * \return 0 on success, -EINVAL if \a outBuffer is NUL, or an error 493 * number if the operation fails for any reason. 494 */ 495 int AHardwareBuffer_recvHandleFromUnixSocket (int socketFd, AHardwareBuffer** outBuffer); 496 497 // __ANDROID_API__ >= 26 498 499 /** 500 * Test whether the given format and usage flag combination is 501 * allocatable. 502 * 503 * If this function returns true, it means that a buffer with the given 504 * description can be allocated on this implementation, unless resource 505 * exhaustion occurs. If this function returns false, it means that the 506 * allocation of the given description will never succeed. 507 * 508 * The return value of this function may depend on all fields in the 509 * description, except stride, which is always ignored. For example, 510 * some implementations have implementation-defined limits on texture 511 * size and layer count. 512 * 513 * \return 1 if the format and usage flag combination is allocatable, 514 * 0 otherwise. 515 */ 516 int AHardwareBuffer_isSupported (const(AHardwareBuffer_Desc)* desc); 517 518 /** 519 * Lock an AHardwareBuffer for direct CPU access. 520 * 521 * This function is the same as the above lock function, but passes back 522 * additional information about the bytes per pixel and the bytes per stride 523 * of the locked buffer. If the bytes per pixel or bytes per stride are unknown 524 * or variable, or if the underlying mapper implementation does not support returning 525 * additional information, then this call will fail with INVALID_OPERATION 526 */ 527 int AHardwareBuffer_lockAndGetInfo ( 528 AHardwareBuffer* buffer, 529 ulong usage, 530 int fence, 531 const(ARect)* rect, 532 void** outVirtualAddress, 533 int* outBytesPerPixel, 534 int* outBytesPerStride); 535 // __ANDROID_API__ >= 29 536 537 // ANDROID_HARDWARE_BUFFER_H 538 539 /** @} */